package android.fwirk.obj;

import java.net.URL;

import android.fwirk.Const;
import android.fwirk.facebook.FacebookClient;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class CustomWebClient extends WebViewClient  {
	ProgressBar pBar;
	WebView view;
	int tID=0;
	
	public CustomWebClient(ProgressBar pBar) {
		this.pBar=pBar;
	}
	
	@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
		if(url.contains("logged_out")) {
			return true;
		}
		return false;
    }
	
	@Override
	public void onPageStarted(WebView view, String url, Bitmap favicon) {
		pBar.setVisibility(View.VISIBLE);
		Log.d("Test s", url);
		if(url.startsWith(FacebookClient.URL_PERMFAILURE)) {
			view.stopLoading();
			view.loadDataWithBaseURL("","<html><body><table height='100%' width='100%'><tr><td align='center'>Permissions not granted.</td></tr><tr><td align='center'> You cannot use Facebook with Fwirk.</td></tr></table></body></html>", "text/html", "utf-8","");
			Const.facebook.setPermitted(false);
		}
		else if(url.startsWith(FacebookClient.URL_PERMSUCCESS)) {
			view.stopLoading();
			view.loadDataWithBaseURL("","<html><body><table height='100%' width='100%'><tr><td align='center'>You can now use Facebook with Fwirk.</td></tr></table></body></html>", "text/html", "utf-8","");
			Const.facebook.setPermitted(true);
		}
		else if(url.startsWith(FacebookClient.URL_LOGSUCCESS)) {
			URL webURL;
			pBar.setVisibility(View.VISIBLE);
			view.stopLoading();
			try {
				webURL = new URL(url);
				String query=webURL.getQuery();
				Const.facebook.setLoggedIn(true);
				Const.facebookToken=query.split("&")[0].replace("auth_token=", "");
			} catch (Exception e) {
				Const.facebook.setLoggedIn(false);
				view.loadUrl(FacebookClient.URL_LOGOUT);
			}
			view.loadUrl(FacebookClient.URL_PERM);
		}
		else if(url.startsWith(FacebookClient.URL_LOGFAILURE)) {
			view.stopLoading();
			Const.facebook.setLoggedIn(false);
			view.loadDataWithBaseURL("","<html><body><table height='100%' width='100%'><tr><td align='center'>Login failed. </td></tr><tr><td align='center'> Redirecting... Please wait.</td></tr></table></body></html>", "text/html", "utf-8","");
			view.loadUrl(FacebookClient.URL_AUTH);
		}
		super.onPageStarted(view, url, favicon);
	}
	
	@Override
	public void onPageFinished(WebView view, String url) {
		Log.d("Test f", url);
		if(url.contains("logged_out")) {
			Const.facebook=new FacebookClient(view.getContext());
			view.loadUrl(FacebookClient.URL_AUTH);
		}
		else if(url.startsWith(FacebookClient.URL_LOGSUCCESS)) {
			pBar.setVisibility(View.VISIBLE);
		}
		else {
			pBar.setVisibility(View.GONE);
		}
		super.onPageFinished(view, url);
	}
	
	@Override
	public void onReceivedError(WebView view, int errorCode,
			String description, String failingUrl) {
		if(failingUrl.startsWith("http://loggingin")) {
			pBar.setVisibility(View.VISIBLE);
		}
		else {
			view.loadDataWithBaseURL("","<html><body><table height='100%' width='100%'><tr><td align='center'>Connection unavailable.</td></tr><tr><td align='center'> Please check network connection.</td></tr></table></body></html>", "text/html", "utf-8","");
		}
	}
}

